﻿namespace WebOptimizer
{
	public partial class SystemContext : ISystemContext
	{
		#region Public Members

		public const string SystemName = "ModelContainer";

		#region ConfigurationContext

		private System.Lazy<global::WebOptimizer.Configuration.IConfigurationContext> _ConfigurationContext = new System.Lazy<global::WebOptimizer.Configuration.IConfigurationContext>(GetConfigurationContext);
		public global::WebOptimizer.Configuration.IConfigurationContext ConfigurationContext
		{
			get
			{
				return _ConfigurationContext.Value;
			}
		}

		#endregion
		#region EntityCache

		private System.Lazy<global::WebOptimizer.Net.Services.V1.Caching.EntityCache> _EntityCache = new System.Lazy<global::WebOptimizer.Net.Services.V1.Caching.EntityCache>();
		public global::WebOptimizer.Net.Services.V1.Caching.EntityCache EntityCache
		{
			get
			{
				return _EntityCache.Value;
			}
		}

		#endregion
		#region EntityFacade

		private System.Lazy<global::WebOptimizer.Net.Services.V1.Model.EntityFacade> _EntityFacade = new System.Lazy<global::WebOptimizer.Net.Services.V1.Model.EntityFacade>();
		public global::WebOptimizer.Net.Services.V1.Model.EntityFacade EntityFacade
		{
			get
			{
				return _EntityFacade.Value;
			}
		}

		#endregion

		#region GetHttpClient

		public Sidvall.Net.IHttpClient GetHttpClient()
		{
			if (Sidvall.SystemContext.Current.HttpClientFactory != null)
				return Sidvall.SystemContext.Current.HttpClientFactory.GetHttpClient(this.ConfigurationContext.HttpClientSettings);
			return null;
		}

		#endregion
		#region GetTransactionScope

		partial void OnGetTransactionScope(Sidvall.Data.ParameterCollection parameters, ref Sidvall.Data.ITransactionScope item);
		public Sidvall.Data.ITransactionScope GetTransactionScope(Sidvall.Data.ParameterCollection parameters)
		{
			Sidvall.Data.ITransactionScope item;

			item = null;
			OnGetTransactionScope(parameters, ref item);
			if (item != null)
				return item;
			if (Sidvall.SystemContext.Current.TransactionScopeFactory != null)
				return Sidvall.SystemContext.Current.TransactionScopeFactory.GetTransactionScope(this.ConfigurationContext.TransactionSettings);
			return null;
		}

		#endregion

		#endregion
		#region Factory Members

		private static readonly System.Lazy<SystemContext> _Current = new System.Lazy<SystemContext>(() => new SystemContext());
		public static ISystemContext Current
		{
			get
			{
				return _Current.Value;
			}
		}

		#endregion
		#region Helper Members

		#region GetConfigurationContext

		private static global::WebOptimizer.Configuration.IConfigurationContext GetConfigurationContext()
		{
			return Sidvall.Reflection.ClassManager.CreateInstance<global::WebOptimizer.Configuration.IConfigurationContext>(null, () => new global::WebOptimizer.Configuration.ConfigurationContext());
		}

		#endregion
		
		#endregion
		#region Constructors

		private SystemContext()
		{
		}

		#endregion
	}
}